package com.yuneec.droneservice.command;

import com.MAVLink.yuneec.msg_mav_rc_cmd_ack;
import com.yuneec.droneservice.model.ControllerSDCard;
import com.yuneec.droneservice.request.IResponse;

/**
 * Created by gjw on 2017/3/27.
 */

public class GetControllerSDcard extends BaseCommand {

    public GetControllerSDcard() {
        super(MAV_RC_COMMAND.MAV_RC_CMD_GET_SDCARD_INFO);
    }

    @Override
    protected short[] body() {
        return null;
    }

    @Override
    public IResponse toResponse(msg_mav_rc_cmd_ack ack) {
        int code = ack.code;
        IResponse<ControllerSDCard> response = new IResponse(ids(), code);
        if (code == MAV_RC_ERROR_CODE.MAV_RC_ERR_SUCCESS) {
            try {
                if (ack.params == null || ack.params.length < 17) {
                    response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
                    return response;
                }
                ControllerSDCard controllerSDCard = new ControllerSDCard();
                controllerSDCard.setInserted(ack.params[0] == 0);
                controllerSDCard.setFree(uIntArrayToLong(ack.params, 1));
                controllerSDCard.setTotal(uIntArrayToLong(ack.params, 9));
                response.setParam(controllerSDCard);
            } catch (Exception e) {
                response.setCode(MAV_RC_ERROR_CODE.MAV_RC_ERR_UNKNOW);
            }
        }
        return response;
    }

}
